package com.young.ums.cloud.api;

import com.young.interfaces.log.annotation.Log;
import com.young.interfaces.ums.model.*;
import com.young.interfaces.ums.service.IUmsOutService;
import com.young.ums.cloud.api.feign.IUmsOutServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 对外服务
 * Created by Administrator on 2019/12/25 0025.
 */
@Service
public class UmsOutServiceImpl implements IUmsOutService {
    /**
     * ums的feign包装
     */
    @Autowired
    IUmsOutServiceFeign umsOutServiceFeign;

    /**
     * 创建资源
     *
     * @param navigate
     */
    @Override
    public void createNavigate(Navigate navigate) {

    }

    /**
     * 根据用户ID获取用户包含的资源列表
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-获取用户资源列表")
    @Override
    public List<Navigate> getNavigateByUserId(String userId) {
        return umsOutServiceFeign.getNavigateByUserId(userId);
    }

    /**
     * 根据用户id获取资源树(获取菜单),暂未实现
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-获取用户资源树")
    @Override
    public List<Navigate> getNavigateAsTreeByUserId(String userId) {
        return umsOutServiceFeign.getNavigateAsTreeByUserId(userId);
    }

    /**
     * 根据用户id获取用户登录信息
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-查询登录信息")
    @Override
    public User getLoginUserById(String userId) {
        return umsOutServiceFeign.getLoginUserById(userId);
    }

    /**
     * 验证单点,即验证token合法性
     *
     * @param token 令牌
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-SSO验证")
    @Override
    public SsoVerifyDTO verifySSO(String token) {
        return umsOutServiceFeign.verifySSO(token);
    }

    /**
     * 查询用户上一次登录的时间
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-查询用户上次登录时间")
    @Override
    public Date getLastLoginTime(String userId) {
        return umsOutServiceFeign.getLastLoginTime(userId);
    }

    /**
     * 根据组织id获取组织信息
     *
     * @param deptId 组织id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-查询组织机构信息")
    @Override
    public Dept getDeptById(String deptId) {
        return umsOutServiceFeign.getDeptById(deptId);
    }

    /**
     * 根据角色id获取角色信息
     *
     * @param roleId 角色id
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-查询角色信息")
    @Override
    public Role getRoleById(String roleId) {
        return umsOutServiceFeign.getRoleById(roleId);
    }

    /**
     * 判断是否存在指定id/account的用户
     * 在创建用户之前做唯一性验证
     *
     * @param userIdOrAccount 用户id或者登陆账号
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-判断账号是否存在")
    @Override
    public boolean existUserIdOrAccount(String userIdOrAccount) {
        return umsOutServiceFeign.existUserIdOrAccount(userIdOrAccount);
    }

    /**
     * 查询用户
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public User getUser(String userId) {
        return null;
    }

    /**
     * 创建用户,供外部调用,id/account/companyId不允许为空
     *
     * @param user 用户信息
     * @return
     * @throws Exception
     */
    @Log("RPC-UMS-新建用户")
    @Override
    public boolean insertUser(User user) {
        return umsOutServiceFeign.insertUser(user);
    }

    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @return int 改动的数据条数,-1表示操作失败
     * @throws Exception
     */
    @Log("RPC-UMS-更新用户")
    @Override
    public int updateUser(User user) {
        return umsOutServiceFeign.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return int 改动的数据条数,-1表示操作失败
     * @throws Exception
     */
    @Log("RPC-UMS-删除用户")
    @Override
    public int deleteUser(String userId) {
        return umsOutServiceFeign.deleteUser(userId);
    }

    /**
     * 校验用户是否有权限访问uri
     *
     * @param userId 用户id
     * @param uri    uri
     * @return
     */
    @Log("RPC-UMS-URI鉴权")
    @Override
    public boolean canAccessUri(String userId, String uri) {
        return umsOutServiceFeign.canAccessUri(userId, uri);
    }
}
